﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Management;
using System.Text;
using WMI.NET.Abstracts;
using WMI.NET.CIM;

namespace WMI.NET.ComputerSystemHardware.MotherboardControllerPort
{
    /// <summary>
    ///  The Win32_BIOS WMI class represents the attributes of the computer system's basic input/output services (BIOS) that are installed on a computer. 
    /// </summary>
    public class Win32_BIOS : CIM_BIOSElement
    {

        private UInt16[] _BiosCharacteristics;
        private string[] _BIOSVersion;
        private string _CurrentLanguage;
        private Nullable<UInt16> _InstallableLanguages;
        private string[] _ListOfLanguages;
        private Nullable<DateTimeOffset> _ReleaseDate;
        private string _SMBIOSBIOSVersion;
        private Nullable<UInt16> _SMBIOSMajorVersion;
        private Nullable<UInt16> _SMBIOSMinorVersion;
        private Nullable<Boolean> _SMBIOSPresent;

        /// <summary>
        /// Array of BIOS characteristics supported by the system as defined by the System Management BIOS Reference Specification.
        /// </summary>
        public UInt16[] BiosCharacteristics { get { return _BiosCharacteristics; } } //TODO enum

        /// <summary>
        /// Windows Server 2003 and Windows XP:  Array of the complete system BIOS information. In many computers there can be several version strings that are stored in the registry and represent the system BIOS information.
        /// </summary>
        public string[] BIOSVersion { get { return _BIOSVersion; } }

        /// <summary>
        /// Name of the current BIOS language.
        /// </summary>
        public string CurrentLanguage { get { return _CurrentLanguage; } }

        /// <summary>
        /// Number of languages available for installation on this system. Language may determine properties such as the need for Unicode and bidirectional text.
        /// </summary>
        public Nullable<UInt16> InstallableLanguages { get { return _InstallableLanguages; } }

        /// <summary>
        /// Array of names of available BIOS-installable languages.
        /// </summary>
        public string[] ListOfLanguages { get { return _ListOfLanguages; } }

        /// <summary>
        /// Release date of the Windows BIOS in the Coordinated Universal Time (UTC) format of YYYYMMDDHHMMSS.MMMMMM(+-)OOO.
        /// </summary>
        public Nullable<DateTimeOffset> ReleaseDate { get { return _ReleaseDate; } }

        /// <summary>
        /// BIOS version as reported by SMBIOS.
        /// </summary>
        public string SMBIOSBIOSVersion { get { return _SMBIOSBIOSVersion; } }

        /// <summary>
        /// Major SMBIOS version number. This property is NULL if SMBIOS is not found.
        /// </summary>
        public Nullable<UInt16> SMBIOSMajorVersion { get { return _SMBIOSMajorVersion; } }

        /// <summary>
        /// Minor SMBIOS version number. This property is NULL if SMBIOS is not found.
        /// </summary>
        public Nullable<UInt16> SMBIOSMinorVersion { get { return _SMBIOSMinorVersion; } }

        /// <summary>
        /// If true, the SMBIOS is available on this computer system.
        /// </summary>
        public Nullable<Boolean> SMBIOSPresent { get { return _SMBIOSPresent; } }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        public Win32_BIOS(ManagementBaseObject mbo)
            : base(mbo)
        {
            _BiosCharacteristics = mbo.GetUshortValueArray("BiosCharacteristics");
            _BIOSVersion = mbo.GetStringValueArray("BIOSVersion");
            _CurrentLanguage = mbo.GetStringValue("CurrentLanguage");
            _InstallableLanguages = mbo.GetUshortValue("InstallableLanguages");
            _ListOfLanguages = mbo.GetStringValueArray("ListOfLanguages");
            _ReleaseDate = mbo.GetDateTimeOffsetValue("ReleaseDate", Consts.DateTimeFormat);
            _SMBIOSBIOSVersion = mbo.GetStringValue("SMBIOSBIOSVersion");
            _SMBIOSMajorVersion = mbo.GetUshortValue("SMBIOSMajorVersion");
            _SMBIOSMinorVersion = mbo.GetUshortValue("SMBIOSMinorVersion");
            _SMBIOSPresent = mbo.GetBoolValue("SMBIOSPresent");
        }

    }

    /// <summary>
    /// 
    /// </summary>
    public class GetWin32_BIOS : Win32Collection<Win32_BIOS>
    {

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ReadOnlyCollection<Win32_BIOS> GetCollection()
        {
            List<Win32_BIOS> tempCollection = new List<Win32_BIOS>();

            foreach (var s in searcher.Get())
            {
                Win32_BIOS tempListItem = new Win32_BIOS(s);
                tempCollection.Add(tempListItem);
            }

            return tempCollection.AsReadOnly();
        }

        /// <summary>
        /// 
        /// </summary>
        public GetWin32_BIOS() : base("SELECT * FROM Win32_BIOS") { }

    }
}
